<template>
	<view class="productList">
		<view class="search-box">
			<uni-icons type="search" size="24" color="#D2D3DA"></uni-icons>
			<input v-model="keyword" placeholder-style="color: #B1B0B3;" placeholder="请输入产品名称" @input="filterbyKeyword" />
		</view>
		<uni-list :border="false">
			<view class="loading-box" v-if="pagingState.loading">
				<image src="@/static/image/loading.gif"></image>
			</view>
			<view class="no-message" v-else-if="products.length === 0">
				<text>暂无产品</text>
			</view>
			<template v-else>
				<uni-list-item v-for="item of products" :clickable="true" :key="item.id" link @click="itemClick(item)">
					<template v-slot:header>
						<view class="image-box" v-if="item.photo">
							<image :src="image_base64(item.photo)" mode="aspectFill"></image>
						</view>
						<view class="default-box" v-else>
							<image mode="widthFix" src="@/static/image/default.png"></image>
						</view>
					</template>
					<template v-slot:body>
						<view class="product-content">
							<text class="product-name">{{item.name}}</text>
							<!-- <view class="product-tags">
								<view class="product-tag-box" v-for="itemY of item.tags" :key="itemY">
									<view class="product-tag">
										<text>{{itemY}}</text>
									</view>
								</view>
							</view> -->
						</view>
					</template>
				</uni-list-item>
			</template>
		</uni-list>
		<view class="loading_more" v-if="pagingState.loading_more">
			<text>加载中...</text>
			<image mode="widthFix" src="@/static/image/loading.gif"></image>
		</view>
		<view class="noMore" v-if="!pagingState.hasMore">
			<text>没有更多了~</text>
		</view>
		<safe-area></safe-area>
	</view>
</template>

<script>
	import {
		productList,
	} from "@/api/search_read.js";
	import _ from "lodash";
	export default {
		data() {
			return {
				// 页面类型 selector：选择性质页面 list: 普通列表
				type: 'list',
				products: [],
				keyword: '',
				// 分页状态
				pagingState: {
					// 页码
					page: 1,
					// 每页条数
					limit: 20,
					// 总条数
					total: 0,
					// 还有更多
					hasMore: true,
					// 加载中
					loading: true,
					// 加载更多中
					loading_more: false,
				}
			}
		},
		onPullDownRefresh() {
			this.refresh();
		},
		onReachBottom: _.debounce(async function() {
			// 加载中时不在发起加载
			if (this.pagingState.loading_more || !this.pagingState.hasMore) {
				return;
			}
			this.pagingState.loading_more = true;
			await this.getList();
			this.$nextTick(() => {
				this.pagingState.loading_more = false;
			})
		}, 1000, {
			leading: false,
			trailing: true
		}),
		onLoad(options) {
			options.type && (this.type = options.type);
			this.refresh();
		},
		methods: {
			// 刷新列表
			async refresh() {
				this.pagingState.page = 1;
				this.pagingState.total = 0;
				this.products = [];
				this.pagingState.hasMore = true;
				this.pagingState.loading = true;
				await this.getList();
				this.pagingState.loading = false;
				uni.stopPullDownRefresh();
			},
			async getList() {
				if (!this.pagingState.hasMore) {
					return Promise.reject(false);
				}
				return new Promise((resolve) => {
					productList({
						limit: this.pagingState.limit,
						offset: this.pagingState.limit * (this.pagingState.page - 1),
						keyword: this.keyword,
					}).then(res => {
						if (res.data?.result) {
							this.pagingState.total = res.data?.result?.length || 0;
							this.products = this.products.concat(res?.data?.result?.records.map(x => {
								// x.tags = [];
								// x.has_aluminum_backing && x.tags.push(x.has_aluminum_backing);
								// x.trim && x.tags.push(x.trim);
								// x.backing_type && x.tags.push(x.backing_type);
								return x;
							}) || []);
							if (this.products.length === this.pagingState.total) {
								this.pagingState.hasMore = false;
							} else {
								this.pagingState.page++;
							}
						} else {
							uni.showToast({
								icon: 'none',
								title: '获取产品列表失败!',
							})
						}
					}).catch(() => {
						uni.showToast({
							icon: 'none',
							title: '系统错误',
						})
					}).finally(() => {
						resolve(true);
					})
				})
			},
			image_base64(photo) {
				return 'data:image/jpeg;base64,' + photo;
			},
			// 关键字过滤产品
			filterbyKeyword: _.debounce(function() {
				this.refresh();
			}, 500, {
				leading: false,
				trailing: true
			}),
			// 点击产品项
			itemClick: _.debounce(function(item) {
				if (this.type === 'selector') {
					uni.$emit('chooseProduct', item);
					uni.navigateBack();
				} else {
					uni.navigateTo({
						url: `/pages/quotation/quotation?id=${item.id}`
					});
				}
			}, 300),
		},
	}
</script>

<style lang="scss" scoped>
	.productList {
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;

		.search-box {
			width: 660rpx;
			margin: 32rpx 0;
			height: 70rpx;
			background-color: white;
			border-radius: 70rpx;
			display: flex;
			align-items: center;
			box-sizing: border-box;
			padding: 0 24rpx;

			uni-icons {
				margin-right: 10rpx;
				flex-shrink: 0;
			}

			input {
				flex: 1;
			}
		}

		uni-list {
			width: 700rpx;
			padding-bottom: 30rpx;

			/deep/.uni-list {
				border-radius: 20rpx;
				overflow: hidden;
			}

			.loading-box {
				width: 100%;
				height: 400rpx;
				display: flex;
				justify-content: center;
				align-items: center;

				image {
					width: 42rpx;
					height: 42rpx;
				}
			}

			.no-message {
				width: 100%;
				height: 400rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 22rpx;
				color: #8E8D8F;
			}

			uni-list-item {
				&.header {
					/deep/.uni-list-item__content-title {
						color: #3F7CFB;
						font-size: 32rpx;
					}

					/deep/.uni-list-item__extra-text {
						color: #8E8D8F;
						font-size: 24rpx;
					}
				}
			}

			.image-box {
				width: 180rpx;
				height: 180rpx;

				image {
					width: 180rpx;
					height: 180rpx;
				}
			}

			.default-box {
				width: 180rpx;
				height: 180rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				background-color: $uni-bg-color;

				image {
					width: 36rpx;
				}
			}

			.product-content {
				padding-left: 24rpx;
				display: flex;
				flex-direction: column;
				// padding-top: 20rpx;

				justify-content: center;

				.product-name {
					font-size: 32rpx;
					margin-bottom: 18rpx;
					word-break: break-all;
				}

				// .product-tags {
				// 	width: 100%;
				// 	display: flex;
				// 	flex-wrap: wrap;

				// 	.product-tag-box {
				// 		padding: 6rpx;

				// 		.product-tag {
				// 			padding: 6rpx 16rpx;
				// 			border-radius: 10rpx;
				// 			background-color: #F0F4FE;
				// 			font-size: 24rpx;
				// 			line-height: 1;
				// 			color: #3F7CFB;
				// 		}
				// 	}
				// }
			}
		}

		.loading_more {
			width: 100%;
			padding: 10rpx 0;
			display: flex;
			justify-content: center;
			align-items: center;

			text {
				font-size: $uni-font-size-sm;
			}

			image {
				width: 30rpx;
				height: 30rpx;
				margin-left: 20rpx;
			}
		}

		.noMore {
			width: 100%;
			padding: 20rpx 0;
			display: flex;
			justify-content: center;
			align-items: center;

			text {
				font-size: $uni-font-size-sm;
			}
		}
	}
</style>